package com.cdte;

import com.cdte.entry.TableIndexDO;
import com.cdte.entry.TableInfoDO;
import com.cdte.gateway.bridge.CommGatewayBridge;
import com.cdte.util.FileUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * 将数据库的ddl转为Excel表格的形式，表格中包含索引及字段信息
 * @author zhoubin
 * @date 2022/10/31 19:27
 */
public class CommonService {

    /**
     * 数据库名
     */
    public static final String datebaseName = "db_cnalgtw";

    /**
     * Excel文件路径
     */
    public static final String filePath = "C:/Users/coip-dev004/Desktop/新建文件夹/db_cispsys_new.xlsx";

    public static void main(String[] args) {

        List<String> list = CommGatewayBridge.selectAllTables();

        File file = FileUtil.createExcel(filePath);

        FileInputStream fis = null;
        XSSFWorkbook workbook = null;
        try {
            fis = new FileInputStream(file);
            workbook = new XSSFWorkbook(fis);

            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setBorderTop(BorderStyle.THIN);

            for (String tableName : list) {
                String sheetName = tableName.substring(2);
                XSSFSheet sheet = workbook.getSheet(sheetName);
                if (Objects.isNull(sheet)) {
                    sheet = workbook.createSheet(sheetName);
                }

                // 设置列宽
                for (int j = 0; j < 7; j++) {
                    sheet.autoSizeColumn(j);
                }

                List<TableIndexDO> tableIndexList = CommGatewayBridge.selectTableIndexInfo(datebaseName, tableName);

                createIndexRow(sheet, tableIndexList, cellStyle);

                List<TableInfoDO> tableInfoList = CommGatewayBridge.selectTableInfo(datebaseName, tableName);
                int infoStart = tableIndexList.size() + 2;

                createColumnRows(sheet, tableInfoList, infoStart, cellStyle);
            }

            workbook.write(new FileOutputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void createColumnRows(XSSFSheet sheet, List<TableInfoDO> tableInfoList, int infoStart, CellStyle cellStyle) {
        XSSFRow infoRowTitle = sheet.createRow(infoStart);
        createSingleCell(cellStyle, infoRowTitle, 0, "字段名");
        createSingleCell(cellStyle, infoRowTitle, 1, "中文名称");
        createSingleCell(cellStyle, infoRowTitle, 2, "数据类型");
        createSingleCell(cellStyle, infoRowTitle, 3, "空值");
        createSingleCell(cellStyle, infoRowTitle, 4, "主键");
        createSingleCell(cellStyle, infoRowTitle, 5, "默认值");
        createSingleCell(cellStyle, infoRowTitle, 6, "取值说明与备注");

        for (int i = 0; i < tableInfoList.size(); i++) {
            TableInfoDO tableInfo = tableInfoList.get(i);
            XSSFRow infoRow = sheet.createRow(infoStart + 1 + i);
            createSingleCell(cellStyle, infoRow, 0, tableInfo.getColumnName());
            createSingleCell(cellStyle, infoRow, 1, tableInfo.getColumnComment());
            createSingleCell(cellStyle, infoRow, 2, tableInfo.getColumnType());
            createSingleCell(cellStyle, infoRow, 3, tableInfo.isNullAble() ? "NULL" : "NOT NULL");
            createSingleCell(cellStyle, infoRow, 4, Objects.equals(tableInfo.getColumnKey(), "PRI") ? "PK" : "");
            createSingleCell(cellStyle, infoRow, 5, tableInfo.getColumnDefault());
            createSingleCell(cellStyle, infoRow, 6, tableInfo.getColumnComment());
        }
    }

    /**
     * 创建表格
     * @param row
     * @param index
     * @param cellValue
     */
    private static void createSingleCell(CellStyle cellStyle, XSSFRow row, int index, String cellValue) {
        XSSFCell cell = row.createCell(index);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(cellValue);
    }


    private static void createIndexRow(XSSFSheet sheet, List<TableIndexDO> tableIndexList, CellStyle cellStyle) {
        XSSFRow row = sheet.createRow(0);
        createSingleCell(cellStyle, row, 0, "索引名称");
        createSingleCell(cellStyle, row, 1, "索引字段");
        createSingleCell(cellStyle, row, 2, "索引类型");
        createSingleCell(cellStyle, row, 3, "用途说明");
        for (int i = 0; i < tableIndexList.size(); i++) {
            TableIndexDO tableIndex = tableIndexList.get(i);
            XSSFRow indexRow = sheet.createRow(1 + i);
            createSingleCell(cellStyle, indexRow, 0, tableIndex.getIndexName());
            createSingleCell(cellStyle, indexRow, 1, tableIndex.getIndexDesc());

            String indexType = "普通索引";
            if (tableIndex.getIndexName().startsWith("uk_")) {
                indexType = "唯一性索引";
            } else if (Objects.equals(tableIndex.getIndexName(), "PRIMARY")) {
                indexType = "主键索引";
            }
            createSingleCell(cellStyle, indexRow, 2, indexType);
            createSingleCell(cellStyle, indexRow, 3, "");
        }
    }

}
